<?php

/**
 * AteacherController
 * 
 * @author
 * @version 
 */

require_once 'Zend/Controller/Action.php';

class AteacherController extends Zend_Controller_Action 
{
	protected $_teacherModel;
	
	public function init()
	{
	     $authService = new Eleven_Service_Authentication();
	           
	    if ($authService->getIdentity()->role != 'Teacher') {
	        $this->_helper->redirector('index','index');
	    }
	    
		$this->_teacherModel = new Eleven_Model_Teacher();
		$this->view->controller = $this->_request->controller;
	}
	
	/**
	 * The default action - show the home page
	 */
	public function indexAction() 
	{
		$topics = $this->_teacherModel->getTopics ( $this->_getParam ( 'page', 1 ), array ('id DESC' ) );
		
		$this->view->assign(array('topics'=>$topics));
	}
	
	public function listtopicAction()
	{
		
	}
	
	public function listcontentAction()
	{
		$contents = $this->_teacherModel->getContentsByTopicId( $this->_getParam ( 'topicId' ), $this->_getParam ( 'page', 1 ), array ('id DESC' ) );

		$this->view->assign(array('contents'=>$contents, 'topicId'=>$this->_getParam ( 'topicId' )));
		
	}
	
	public function editcontentAction()
	{
		$this->view->contentForm = $this->getContentForm($this->_getParam('contentId'),null);
	}
	
	public function deletecontentAction()
	{
		if ($this->_teacherModel->deleteContentById ( $this->_getParam ( 'contentId' )))
	    {
	        return $this->_helper->redirector('index');
	    }else{
	        return $this->_helper->redirector('index');
	    }
	}
	
	public function savecontentAction()
	{
		$request = $this->getRequest();
		if (!$request->isPost()) 
		{
			return $this->_helper->redirector('index');
  		}
  		
  		if (false === $this->_teacherModel->saveContentEdit($request->getPost())) 
  		{
  			$this->view->contentForm = $this->getContentForm($request->getPost('id'));
     		return $this->render('editcontent');
  		}
  		
  		return $this->_helper->redirector('index');
	}
	
	public function addtopicAction() 
	{
		$this->view->topicForm = $this->getTopicForm($this->_getParam('topicId'),null);
	}
	
	public function edittopicAction()
	{
		$this->view->topicForm = $this->getTopicForm($this->_getParam('topicId'),null);
	}
	
	public function deletetopicAction()
	{
		if ($this->_teacherModel->deleteTopicById ( $this->_getParam ( 'topicId' )))
	    {
	        return $this->_helper->redirector('index');
	    }else{
	        return $this->_helper->redirector('index');
	    }
	}
	
	public function saveAction()
	{
		$request = $this->getRequest();
		if (!$request->isPost()) 
		{
			return $this->_helper->redirector('index');
  		}
  		
  		if (false === $this->_teacherModel->saveTopicEdit($request->getPost())) 
  		{
  		    $this->view->topicForm = $this->getTopicForm($this->_getParam('topicId'),null);
     		return $this->render('edittopic');
  		}
  		
  		return $this->_helper->redirector('index');
	}
	
    public function getTopicForm($topicId)
	{
		$urlHelper = $this->_helper->getHelper('url');

        $this->_forms['topic'] = $this->_teacherModel->getForm('teacherTopic');
        $this->_forms['topic']->setAction($urlHelper->url(array('action' => 'save')));
        
        $this->_forms['topic']->setMethod('post');
        
        if('' != $this->_getParam('topicId'))
        {
	        $topic = $this->_teacherModel->getCurrentTopic($topicId);
	        
	        $this->_forms['topic']->getElement('id')->setValue($topic->id);
	        $this->_forms['topic']->getElement('title')->setValue($topic->title);
	        $this->_forms['topic']->getElement('content')->setValue($topic->content);
        }else {
        	$this->_forms['topic']->removeElement('id');
        }        
        
        return $this->_forms['topic'];
	}
	
	public function getContentForm($contentId)
	{
		$urlHelper = $this->_helper->getHelper('url');

        $this->_forms['content'] = $this->_teacherModel->getForm('teacherContent');
        $this->_forms['content']->setAction($urlHelper->url(array('action' => 'savecontent')));
        
        $this->_forms['content']->setMethod('post');
        
        $content = $this->_teacherModel->getContentById($contentId);
        
        $this->_forms['content']->getElement('id')->setValue($content->id);
        $this->_forms['content']->getElement('title')->setValue($content->title);
        $this->_forms['content']->getElement('content')->setValue(stripslashes($content->content));
        $this->_forms['content']->getElement('type')->setValue($content->type);
        
        return $this->_forms['content'];
	}
	
    public function topAction()
	{
        $tops = $this->_request->getParam('select');
        if (count($tops))
        {
            $this->_teacherModel->setTops($tops);
            $topicId = $this->_request->getParam('topicId');
        }else {
            $tops = array($this->_request->getParam('id'));
            $content = $this->_teacherModel->getContentById($this->_request->getParam('id'));
            $this->_teacherModel->setTops($tops, true);
            $topicId = $content->topicid;
        }
        
        return $this->_helper->redirector->gotoSimple(
        'listcontent','ateacher', null, array('topicId'=>$topicId));
	}

}

